package com.wing.productionPlan.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.wing.common.utils.UniqueIdGeneratorUtil;
import com.wing.productionPlan.dao.ProProductDeliveryOrderDao;
import com.wing.productionPlan.model.entity.ProProductDeliveryOrder;

import com.wing.productionPlan.model.response.ProProductDeliveryOrderVo;
import com.wing.productionPlan.service.ProProductDeliveryOrderService;
import com.wing.web.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;


/**
 * 提货订单-ServiceImpl
 *
 * @author
 */
@Service
@Slf4j
public class ProProductDeliveryOrderServiceImpl extends ServiceImpl<ProProductDeliveryOrderDao, ProProductDeliveryOrder> implements ProProductDeliveryOrderService {

    /**
     * 查询提货订单列表
     * @param pg
     * @param map
     * @return
     */
    @Override
    public Page<ProProductDeliveryOrderVo> pageList4app(Page<ProProductDeliveryOrderVo> pg, Map map) {
        List<ProProductDeliveryOrderVo> tasks = this.baseMapper.pageList4app(pg,map);
        pg.setRecords(tasks);
        return pg;
    }

    /**
     * 添加提货单信息
     * @param proProductDeliveryOrder
     * @return
     */
    @Override
    public int add(ProProductDeliveryOrder proProductDeliveryOrder) {
        String recordNo = UniqueIdGeneratorUtil.generateUniqueId("TH", "yyyyMMdd", 1000, 9999);
        proProductDeliveryOrder.setCreator(JwtUtils.getLoginUserId());
        proProductDeliveryOrder.setCreatorName(JwtUtils.getLoginUsername());
        proProductDeliveryOrder.setRecordNo(recordNo);
        proProductDeliveryOrder.setCreateTime(LocalDateTime.now());
        boolean save = save(proProductDeliveryOrder);
        if (save){
            return 1;
        }
        return 0;
    }

    /**
     * 修改提货信息
     * @param proProductDeliveryOrder
     * @return
     */
    @Override
    public int updateInfo(ProProductDeliveryOrder proProductDeliveryOrder) {
        ProProductDeliveryOrder byId = getById(proProductDeliveryOrder.getId());
        if(ObjectUtil.isEmpty(byId)){
            return 0;
        }
        CopyOptions copyOptions = CopyOptions.create()
                .setEditable(ProProductDeliveryOrder.class)
                .setIgnoreError(true)
                .setIgnoreNullValue(true);
        BeanUtil.copyProperties(proProductDeliveryOrder, byId, copyOptions);
        byId.setUpdateTime(LocalDateTime.now());
        byId.setModifier(JwtUtils.getLoginUserId());
        byId.setModifierName(JwtUtils.getLoginUsername());
        boolean b = updateById(byId);
        if (b){
            return 1;
        }
        return 0;
    }


    @Override
    public List<Map<String, Object>> getCodeToIdMapForProduct() {
        return this.baseMapper.getCodeToIdMapForProduct();
    }
}
